package View.Student;

import Service.DormService;
import Service.ServiceImpl.DormServiceImpl;
import Service.ServiceImpl.StudentServiceImpl;
import Service.StudentService;
import View.Index.Index_admin;
import domain.Dorm;
import domain.Student;
import domain.User;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

public class AddStudent extends JFrame {

    public AddStudent(User user){
        init();
        componentInit(user);
    }

    public void init(){
        this.setTitle("添加学生");
        this.setSize(430,415);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(User user){
        //------------------JLabel--------------------
        JLabel label1= new JLabel();
        label1.setText("学生号:");
        label1.setBounds(83,28,65,30);
        JLabel label2= new JLabel();
        label2.setText("姓名:");
        label2.setBounds(83,81,65,30);
        JLabel label3= new JLabel();
        label3.setText("性别:");
        label3.setBounds(82,137,65,30);
        JLabel label4= new JLabel();
        label4.setText("班级:");
        label4.setBounds(81,191,65,30);
        JLabel label5= new JLabel();
        label5.setText("宿舍号:");
        label5.setBounds(84,242,65,30);


        //------------------JTextField--------------------
        JTextField field1= new JTextField();
        field1.setText("");
        field1.setBorder(new LineBorder(Color.lightGray));
        field1.setBounds(160,29,141,30);
        JTextField field2= new JTextField();
        field2.setText("");
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setBounds(158,83,137,30);
        String[] item = new String[3];
        item[0]="请选择性别";
        item[1]="男";
        item[2]="女";
        JComboBox comboBox1= new JComboBox(item);
        comboBox1.setBounds(158,138,135,30);
        JTextField field4= new JTextField();
        field4.setText("");
        field4.setBorder(new LineBorder(Color.lightGray));
        field4.setBounds(158,192,137,30);
        DormService service = new DormServiceImpl();
        List<Dorm> dorms = service.dormList();
        Vector<String> item2= new Vector<>();
        item2.add("请选择你的宿舍");
        for (int i = 0; i < dorms.size(); i++) {
            Dorm dorm = dorms.get(i);
            item2.add(dorm.getDormnum());
        }

        JComboBox comboBox2= new JComboBox(item2);
        comboBox2.setBounds(159,243,134,30);

        //------------------JButton--------------------
        JButton button1= new JButton();
        button1.setText("添加");
        button1.setBounds(70,309,95,27);
        JButton button2= new JButton();
        button2.setText("退出");
        button2.setBounds(181,309,95,27);





        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label4);
        this.add(label5);
        this.add(field1);
        this.add(field2);
        this.add(comboBox1);
        this.add(field4);
        this.add(comboBox2);
        this.add(button1);
        this.add(button2);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Student student = new Student();
                student.setStunum(field1.getText());
                student.setName(field2.getText());
                student.setSex((String) comboBox1.getSelectedItem());
                student.setClasses(field4.getText());
                student.setDormnum((String) comboBox2.getSelectedItem());

                StudentService service = new StudentServiceImpl();
                service.addStudent(student);

                JFrame frame = new JFrame();
                JOptionPane.showMessageDialog(frame,"添加成功!");
                field1.setText("");
                field2.setText("");
                comboBox1.setSelectedIndex(0);
                field4.setText("");
                comboBox2.setSelectedIndex(0);
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Index_admin(user).setVisible(true);
            }
        });































    }
}
